/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Customer.java
 *
 * Created on Dec 9, 2009, 6:27:31 PM
 */

package GUI;

import Business.Customer_BS;
import Business.Customer_MD;
import DAO.Customer_DAO;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author anhvtn_b00545
 */
public class Customer extends javax.swing.JPanel {

    public Customer() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        lblTypeCustomer = new javax.swing.JLabel();
        cbbTypeCustomer = new javax.swing.JComboBox();
        lblNameCustomer = new javax.swing.JLabel();
        txtNameCustomer = new javax.swing.JTextField();
        btSearch = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbCustomer = new javax.swing.JTable();
        btDeleteCustomer = new javax.swing.JButton();
        btACreateCustomer = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 602, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Filter"));

        lblTypeCustomer.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblTypeCustomer.setText("Type: ");

        cbbTypeCustomer.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "All", "General Customers", "Dealers", "Who Sale Retailers", " " }));

        lblNameCustomer.setFont(new java.awt.Font("Tahoma", 1, 11));
        lblNameCustomer.setText("Name: ");

        btSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Filter.png"))); // NOI18N
        btSearch.setText("Search");
        btSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTypeCustomer)
                .addGap(18, 18, 18)
                .addComponent(cbbTypeCustomer, 0, 147, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(lblNameCustomer)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNameCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btSearch)
                .addGap(96, 96, 96))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(cbbTypeCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lblTypeCustomer)
                .addComponent(lblNameCustomer)
                .addComponent(txtNameCustomer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btSearch))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Customer List"));

        tbCustomer.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Code", "Name", "Phone", "Address"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbCustomer.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbCustomerMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbCustomer);

        btDeleteCustomer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Button-Delete-icon.png"))); // NOI18N
        btDeleteCustomer.setText("Delete");
        btDeleteCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeleteCustomerActionPerformed(evt);
            }
        });

        btACreateCustomer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Img/Add.png"))); // NOI18N
        btACreateCustomer.setText("Create");
        btACreateCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btACreateCustomerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(375, Short.MAX_VALUE)
                .addComponent(btACreateCustomer)
                .addGap(43, 43, 43)
                .addComponent(btDeleteCustomer)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 596, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 363, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btACreateCustomer)
                    .addComponent(btDeleteCustomer))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btACreateCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btACreateCustomerActionPerformed
        // TODO add your handling code here:
        CreateCustomer customer = new CreateCustomer(null, true);
        customer.setVisible(true);
    }//GEN-LAST:event_btACreateCustomerActionPerformed

    private void btDeleteCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeleteCustomerActionPerformed
        if (JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this customer?")
                == JOptionPane.YES_OPTION) {
            int row = tbCustomer.getSelectedRow();
            Customer_BS customer = getCustomerAtRow(row);
            Customer_DAO.delete(customer.getcID());
            
            ((Customer_MD) tbCustomer.getModel()).deleteCustomer(customer);
        }
    }//GEN-LAST:event_btDeleteCustomerActionPerformed

    private void btSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSearchActionPerformed
        List<Customer_BS> customers = Customer_DAO.getCustomers(cbbTypeCustomer.getSelectedIndex() - 1,
                txtNameCustomer.getText());
        tbCustomer.setModel(new Customer_MD(customers));
    }//GEN-LAST:event_btSearchActionPerformed

    private void tbCustomerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbCustomerMouseClicked
        if (evt.getClickCount() > 1) {
            int row = tbCustomer.getSelectedRow();
            Customer_BS selectedCustomer = getCustomerAtRow(row);

            CustomerDetails CD = new CustomerDetails(null, true);
            CD.setCustomer(selectedCustomer);
            CD.setVisible(true);

            ((Customer_MD) tbCustomer.getModel()).updateCustomer(selectedCustomer);
        }
    }//GEN-LAST:event_tbCustomerMouseClicked

    private Customer_BS getCustomerAtRow(int rowIndex) {
        Customer_MD model = (Customer_MD) tbCustomer.getModel();
        return model.getCustomer(rowIndex);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btACreateCustomer;
    private javax.swing.JButton btDeleteCustomer;
    private javax.swing.JButton btSearch;
    private javax.swing.JComboBox cbbTypeCustomer;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblNameCustomer;
    private javax.swing.JLabel lblTypeCustomer;
    private javax.swing.JTable tbCustomer;
    private javax.swing.JTextField txtNameCustomer;
    // End of variables declaration//GEN-END:variables

}
